<template>
  <base-layout>
    <template #searchPanel>
      <search-panel
        v-slot="{ form }"
        :selectPer="'thrk:page'"
        @search="search"
        :showSearchButton="true"
      >
        <el-select
          clearable
          style="width: 200px"
          v-model="form.status"
          placeholder="采购退货状态"
        >
          <el-option
            v-for="item in getDict(['采购退货状态'])['采购退货状态']"
            :key="item.index"
            :value="item.value"
            :label="item.text"
          />
        </el-select>
        <CarModel style="width: 350px" v-model="modelList" ref="carModel" />
        <el-date-picker
          v-model="form.inTime"
          type="datetimerange"
          start-placeholder="退货时间"
          end-placeholder="退货时间"
          clearable
          value-format="yyyy-MM-dd HH:mm:ss"
          :default-time="['00:00:00', '23:59:59']"
        />
        <el-input
          v-model="form.client"
          clearable
          placeholder="请输入退货编号"
          style="width: 200px"
        />
        <el-input
          v-model="form.vin"
          clearable
          placeholder="请输入vin码"
          style="width: 200px"
        />
      </search-panel>
    </template>
    <template #contentPanel>
      <data-table
        :value="tableData"
        :total-count="totalCount"
        :has-action="true"
        :table-column-attributes="tableColumnAttributes"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        @row-dblclick="handleDetails"
        :width="160"
      >
        <template #action="{ row }">
          <i
            class="el-icon-right"
            title="退货入库"
            style="margin-left: 15px; cursor: pointer"
            v-permission="'thrk:status'"
            @click="handleRevoke(row)"
          ></i>
          <i
            class="el-icon-edit"
            title="修改"
            style="margin-left: 15px; cursor: pointer"
            v-permission="'thrk:save'"
            @click="updateStock(row)"
          ></i>
        </template>
      </data-table>
      <moduleDialog
        v-if="detailData"
        :visible="visible"
        @set-visible="setVisible"
        :detailData="detailData"
      />
    </template>
  </base-layout>
</template>
<script>
import baseMixin from '@/mixins/base'
import { CarModel } from '@/components/index'
import moduleDialog from './components/moduleDialog'
import {
  dataList,
  details,
  findByStatus,
} from '@/api/pre-sale/newCarStock/putInStorageManagement/returnInventory'
import { getDict } from '@/utils/index'

export default {
  components: {
    CarModel,
    moduleDialog,
  },
  mixins: [
    baseMixin({
      idName: 'id',
      findData: dataList,
      dataName: ['content', 'totalRecords'],
      format(res) {
        return res.map((data) => {
          data.brandSeriesModel =
            data.carBrandName + '/' + data.seriesName + '/' + data.modelName
          data.cname = data.clientName + '/' + data.clientPhone
          return data
        })
      },
    }),
  ],
  data() {
    return {
      detailData: {},
      detailId: '',
      visible: false,
      tableDataTwo: [],
      modelList: [],
      getDict: getDict,
      form: {
        vin:'',
        brandId: '',
        seriesId: '',
        modelId: '',
      },
      tableColumnAttributes: [
        {
          name: '门店',
          value: 'storeName',
          type: 'text',
        },
        {
            name: '新车订单号',
          value: 'xcCode',
          type: 'text',
        },
        {
          name: '退货编号',
          value: 'code',
          type: 'text',
        },
        {
          name: '退货仓库',
          value: 'storageName',
          type: 'text',
        },
        {
          name: '品牌/车系/车型',
          value: 'brandSeriesModel',
          type: 'text',
        },
        {
          name: '车架号',
          value: 'vin',
          type: 'text',
        },
        {
          name: '供应商',
          value: 'supplierName',
          type: 'text',
        },
        {
          name: '进口来源',
          value: 'source',
          type: 'text',
        },
        {
          name: '退货客户/联系方式',
          value: 'cname',
          type: 'text',
        },
        {
          name: '销售人员',
          value: 'sellUserName',
          type: 'text',
        },
        {
          name: '退货原因',
          value: 'returnRemark',
          type: 'dict',
          dict: '订单类别',
        },
        {
          name: '库存状态',
          value: 'status',
          type: 'text',
        },
        {
          name: '购买日期',
          value: 'gmrq',
          type: 'text',
        },
        {
          name: '录入时间',
          value: 'createTime',
          type: 'text',
        },
      ],
      dict: {},
    }
  },
  methods: {
    // 查看
    async handleDetails(row) {
      this.visible = true
      const { data } = await details(row.id)
      this.detailData = data
    },
    setVisible(val) {
      this.visible = val
      this.detailData = {}
    },
    // 撤销
    handleRevoke(row) {
      this.$confirm('是否确定退货入库?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(async () => {
          await findByStatus(row.id)
          this.setTableData()
        })
        .catch(() => {})
    },
    updateStock({ id }) {
      this.$router.push({
        name: 'editReturnInventory',
        params: {
          id,
        },
      })
    },
    // 查询
    search(form) {
      if (this.modelList.length > 0) {
        form.brandId = this.modelList[0]
        form.seriesId = this.modelList[1]
        form.modelId = this.modelList[3]
      } else {
        form.brandId = ''
        form.seriesId = ''
        form.modelId = ''
      }
      if (form.inTime && form.inTime.length > 0) {
        form.inBoundMinTime = form.inTime[0]
        form.inBoundMaxTime = form.inTime[1]
      } else {
        form.inBoundMinTime = ''
        form.inBoundMaxTime = ''
      }
      delete form.inTime
      this.searchOptions = form
      this.currentPage = 1
      this.setTableData()
    },
  },
}
</script>
